package org.vectrics.recruiter.struts.admin;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Category;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.user.Interview;
import org.vectrics.struts.Request;

public class ShowInterviewAction extends Action {
    private static Category log = Category.getInstance(ShowInterviewAction.class);

    public static String LOCATION_PARAM = "location";

    private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

    private SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm");

    private SimpleDateFormat amPmFormat = new SimpleDateFormat("a");

    @Override
    public ActionForward execute(final ActionMapping mapping, ActionForm form, final HttpServletRequest request,
            final HttpServletResponse response) throws Exception {
        InterviewForm interviewForm = (InterviewForm) form;
        if (form == null) {
            log.info("form is null");
            interviewForm = new InterviewForm();
            form = interviewForm;
        } else {
            log.info("Form class = " + form.getClass().getName());
        }
        String interviewId = request.getParameter("id");
        Interview interview = null;
        if (interviewId != null && interviewId.trim().length() > 0) {
            interview = RecruitServiceLocator.getRecruitUserService().findInterviewById(new Long(interviewId));
        }

        Date startDate = null;
        Date endDate = null;
        if (interview == null) {
            log.debug("interview is null");
            startDate = new Date();
            interviewForm.setMinutes("30");
        } else {
            startDate = interview.getDateStart();
            endDate = interview.getDateEnd();
            interviewForm.setPersonId(interview.getPerson().getId().toString());
            interviewForm.setJobId(interview.getJob().getId().toString());
            interviewForm.setDescription(interview.getDescription());

            interviewForm.setFollowUp(interview.getFollowUp());
            interviewForm.setNotes(interview.getNotes());
            interviewForm.setId(interview.getId().toString());
            if (interview.getResultType() != null) {
                interviewForm.setResultCode(interview.getResultType().getCode());
            }

            interviewForm.setResult(interview.getResult());
            interviewForm.setStatusCode(interview.getStatusType().getCode());
            if (interview.getLeadPerson() != null) {
                interviewForm.setInterviewerId(interview.getLeadPerson().getId().toString());
            }

            if (log.isDebugEnabled()) {
                log.debug("id = " + interviewForm.getId());
                log.debug("interviewerId = " + interviewForm.getInterviewerId());
            }

        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int amPm = calendar.get(Calendar.AM_PM);
        if (hour == 0 && amPm > 0) {
            hour = 12;
        }
        log.info("Hour = " + hour);
        log.info("Minute = " + minute);
        interviewForm.setStartHour("" + hour);
        interviewForm.setStartMinute("" + minute);
        log.info("AM_PM = " + amPm);

        boolean pm = amPm > 0;

        log.info("PM = " + pm);
        interviewForm.setStartHour("" + hour);
        interviewForm.setStartMinute("" + minute);
        if (pm) {
            interviewForm.setStartAmPm("pm");
        } else {
            interviewForm.setStartAmPm("am");
        }

        SimpleDateFormat roundToDateFormat = CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(
                request.getLocale());

        interviewForm.setStartDate(roundToDateFormat.format(startDate));

        if (endDate != null) {
            long millis = endDate.getTime() - startDate.getTime();
            long minutesEllapsed = millis / (1000 * 60);
            interviewForm.setMinutes("" + minutesEllapsed);
            log.info("Minutes ellapsed: " + minutesEllapsed);
        } else {

        }

        // storeStatusTypesAsRequestAttribute(request);

        return mapping.findForward("success");
    }

    public static void storeStatusTypesAsRequestAttribute(final Request request) {
        Collection statusTypes = RecruitServiceLocator.getPersonService().findAllPersonEncounterStatusTypes();
        request.setAttribute("statusTypes", statusTypes);
    }

}
